﻿using System;
using System.Collections.Generic;
using System.Collections;

using System.Web;
using System.Xml;
using System.IO;

/// <summary>
/// Summary description for utility
/// </summary>
public class utility2
{
	public utility2()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static RE_User PharseXML(string XMLUsuario) { 
        RE_User result=new RE_User();
        try {
            XmlDocument Xdoc = new XmlDocument();
            
            Xdoc.Load(new StringReader(XMLUsuario));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name){
                    case "usuarioid": {
                        result.userID = campo.InnerText;
                        break;
                    }
                    case "contrasena": {
                        result.pss = campo.InnerText;
                        break;
                    }
                    case "aplicacionid": {
                        result.appl = campo.InnerText;
                        break;
                    }
                    case "perfilid": {
                        result.perfil = campo.InnerText;
                        break;
                    }
                }
            }
        } catch (Exception e){
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }

    public static RE_User PharseXML_Registro(string XMLUsuario)
    {
        RE_User result = new RE_User();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLUsuario));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "user":
                        {
                            result.userID = campo.InnerText;
                            break;
                        }
                    case "pass":
                        {
                            result.pss = campo.InnerText;
                            break;
                        }
                    case "name":
                        {
                            result.nombre = campo.InnerText;
                            result.nombre = campo.InnerText;
                            break;
                        }
                    case "lastname":
                        {
                            result.apellido = campo.InnerText;
                            break;
                        }
                    case "comp-name":
                        {
                            result.compania = campo.InnerText;
                            break;
                        }
                    case "alias":
                        {
                            result.alias = campo.InnerText;
                            break;
                        }
                    case "email":
                        {
                            result.email = campo.InnerText;
                            break;
                        }
                    case "title":
                        {
                            result.title = campo.InnerText;
                            break;
                        }
                    case "estado":
                        {
                            result.estado = int.Parse(campo.InnerText);
                            break;
                        }
                    case "aplicacionid":
                        {
                            result.appl = campo.InnerText;
                            break;
                        }
                    case "perfilid":
                        {
                            result.perfil = campo.InnerText;
                            break;
                        }
                    case "existe":
                        {
                            result.existe = 1;
                            break;
                        }
                    case "logo":
                        {
                            result.logo = campo.InnerText;
                            break;
                        }
                }
                if (campo.Name.Equals("direcciones"))
                {
                    XmlNodeList direcciones = campo.ChildNodes;
                    foreach (XmlElement direccion in direcciones)
                    {
                        RE_Direccion dir = new RE_Direccion();
                        foreach (XmlElement elemento in direccion)
                        {
                            if (!elemento.InnerText.Equals(""))
                            {
                                switch (elemento.Name)
                                {
                                    case "uidID":
                                        {
                                            dir.uinID = int.Parse(elemento.InnerText);
                                            break;
                                        }
                                    case "add1":
                                        {
                                            dir.stnumber = elemento.InnerText;
                                            break;
                                        }
                                    case "add2":
                                        {
                                            dir.stadd = elemento.InnerText;
                                            break;
                                        }
                                    case "city":
                                        {
                                            dir.city = elemento.InnerText;
                                            break;
                                        }
                                    case "state":
                                        {
                                            dir.state = elemento.InnerText;
                                            break;
                                        }
                                    case "zip":
                                        {
                                            dir.zip = elemento.InnerText;
                                            break;
                                        }
                                    case "office_phone":
                                        {
                                            dir.telof = elemento.InnerText;
                                            break;
                                        }
                                    case "cell_phone":
                                        {
                                            dir.telcel = elemento.InnerText;
                                            break;
                                        }
                                    case "fax":
                                        {
                                            dir.fax = elemento.InnerText;
                                            break;
                                        }
                                    case "tipo":
                                        {
                                            dir.tipodir = int.Parse(elemento.InnerText);
                                            break;
                                        }
                                }
                            }
                            if (elemento.Name.Equals("credit_card"))
                            {
                                XmlNodeList tarjetas = elemento.ChildNodes;
                                RE_CreditCard cc = new RE_CreditCard();
                                foreach (XmlElement tarjeta in tarjetas)
                                {
                                    
                                    if (!tarjeta.InnerText.Equals(""))
                                    {
                                        switch (tarjeta.Name)
                                        {
                                            case "tacID":
                                                {
                                                    cc.tacID = int.Parse(tarjeta.InnerText);
                                                    break;
                                                }
                                            case "namecreditcard":
                                                {
                                                    cc.name = tarjeta.InnerText;
                                                    break;
                                                }
                                            case "creditcard":
                                                {
                                                    cc.number = tarjeta.InnerText;
                                                    break;
                                                }
                                            case "expdate":
                                                {
                                                    cc.expdate = tarjeta.InnerText;
                                                    break;
                                                }
                                            case "ccv":
                                                {
                                                    cc.ccv = int.Parse(tarjeta.InnerText);
                                                    break;
                                                }
                                        }
                                    }
                                }
                                if (dir.tarjetas == null) dir.tarjetas = new ArrayList();
                                dir.tarjetas.Add(cc);
                            }
                        }
                        if (result.direcciones == null) result.direcciones = new ArrayList();
                        result.direcciones.Add(dir);
                    }
                }
            }
        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }



    internal static RE_GenericBean2 PharseXML_Company(string XMLCompany)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLCompany));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                    case "estado":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }
                   
                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }
    internal static RE_GenericBean2 PharseXML_Account(string XMLAccount)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLAccount));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                    case "initial":
                        {
                            result.strC2 = campo.InnerText;
                            break;
                        }

                    case "estado":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }

                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }
    internal static RE_GenericBean2 PharseXML_TEmployee(string XMLTEmployee)
    {
        RE_GenericBean2 result = new RE_GenericBean2();
        try
        {
            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(new StringReader(XMLTEmployee));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "name":
                        {
                            result.strC1 = campo.InnerText;
                            break;
                        }
                   
                    case "catalogo":
                        {
                            result.intC1 = int.Parse(campo.InnerText);
                            break;
                        }

                }
            }

        }
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            return null;
        }
        return result;
    }

    internal static int PharseXML_UserData(string XMLDATA)
    {
        RE_GenericBean2 result;
        int resp = 0;
        int group=0;
        int[] matperfil= new int[50];
        int[] matcompany= new int[50];
        int[] matoption = new int[50];
        int[] matstate = new int[50];
        int[] matcounty = new int[50];
        string[] matviewdef = new string[50];
        string [][] matvprices= new string[50][];
        string [][] matcprices= new string[50][];
        string usuarioid="";
        int posperfil = 0;
        int poscompany=0;
        int posoption = 0;
        int poscounty = 0;
        int posviewdef = 0;
        int posvprices=0;
        int poscprices=0;
        int posstate = 0;
        try
        {
            XmlDocument Xdoc = new XmlDocument();
            result= new RE_GenericBean2();
            Xdoc.Load(new StringReader(XMLDATA));
            XmlElement root = Xdoc.DocumentElement;
            foreach (XmlElement campo in root)
            {
                switch (campo.Name)
                {
                    case "group":
                        {
                            group = int.Parse(campo.InnerText);
                            break;
                         }
                    case "userid":
                        {
                            usuarioid=(campo.InnerText);
                            break;
                         }
                }
                if (campo.Name.Equals("perfiles"))
                {
                    
                    XmlNodeList perfiles = campo.ChildNodes;
                    posperfil = 0;
                    foreach (XmlElement perfil in perfiles)
                    {
                       
                                switch (perfil.Name)
                                {
                                    case "perfil":
                                        {
                                            matperfil[posperfil] = int.Parse(perfil.InnerText);
                                            posperfil++;
                                            break;
                                        }
                                }
                            }
                    if (group == 3) { DB2.delete("tbl_usuario_perfil", "upe_usu_id", usuarioid,0,1); DB2.setPerfilUsuario(usuarioid, matperfil,posperfil,1); }
                    else if (group== 1) {matperfil[0]=1; posperfil=1; DB2.delete("tbl_usuario_perfil", "upe_usu_id", usuarioid,0,1); DB2.setPerfilUsuario(usuarioid, matperfil,posperfil,1); }
                        }
                
                
                
                if (campo.Name.Equals("ClientCompanies"))
                {
                    poscompany=0;
                    XmlNodeList companies = campo.ChildNodes;
                    foreach (XmlElement company in companies)
                    {

                        switch (company.Name)
                        {
                            case "company":
                                {
                                    matcompany[poscompany]= int.Parse(company.InnerText);
                                    poscompany++;
                                    break;
                                }
                        }
                    }
                     DB2.delete("tbl_usuario_company", "uco_usu_id", usuarioid,0,1);
                     DB2.setCompanyporUsuario(usuarioid, matcompany,poscompany);
                }

                if (campo.Name.Equals("OptionsAccess"))
                {
                    posoption = 0;
                    int permiso = 0;
                    XmlNodeList options = campo.ChildNodes;
                    foreach (XmlElement option in options)
                    {

                        switch (option.Name)
                        {
                            case "option":
                                {
                                   matoption[posoption]=  int.Parse(option.InnerText);
                                   posoption++;
                                   permiso += int.Parse(option.InnerText);
                                    break;
                                }
                        }
                    }
                    
                    DB2.updatedata("tbl_usuario", "usu_permiso","usu_id",usuarioid,permiso,"",1);
                }

                if (campo.Name.Equals("ViewDefault"))
                {
                    XmlNodeList viewdefaul = campo.ChildNodes;
                    string where = "and (";
                    int p = 0;
                    foreach (XmlElement viewde in viewdefaul)
                    {

                        switch (viewde.Name)
                        {
                            case "view":
                                {
                                    
                                    if (p == 1) { where += " or "; }else{ p = 1; }
                                    where += " client_id= '" + viewde.InnerText+"' ";
                                    break;
                                }
                        }
                    }
                    where+= " )";
                    DB2.updatedata("tbl_usuario", "usu_where", "usu_id", usuarioid, 0, where, 1);
                }

                if (campo.Name.Equals("RestrictedAccess"))
                {
                    int permiso=0;
                    
                    XmlNodeList restrictedacc = campo.ChildNodes;
                    foreach (XmlElement res in restrictedacc)
                    {

                        switch (res.Name)
                        {
                            case "option":
                                {
                                    
                                  permiso+= int.Parse(res.InnerText);   
                                  
                                    break;
                                }
                        }

                    }

                    DB2.updatedata("tbl_usuario", "usu_restricted", "usu_id", usuarioid, permiso, "", 0);
                }
                if (campo.Name.Equals("FilterView"))
                {
                    XmlNodeList filterview = campo.ChildNodes;
                    foreach (XmlElement fil in filterview)
                    {

                        switch (fil.Name)
                        {
                            case "product":
                                {
                                    result.intC1 = int.Parse(fil.InnerText);
                                    break;
                                }
                        }
                    }
                }

                if (campo.Name.Equals("CompanyAccess"))
                {
                    string where = "and ("; int p = 0;
                    XmlNodeList clientcompanies = campo.ChildNodes;
                    foreach (XmlElement company in clientcompanies)
                    {

                        switch (company.Name)
                        {
                            case "company":
                                {
                                    if (p == 1) { where += " or "; } else { p = 1; }
                                    where += " company = " + company.InnerText;
                                    break;
                                }
                        }
                    }
                    where += " )";
                    DB2.updatedata("tbl_usuario", "usu_where2", "usu_id", usuarioid, 0, where, 1);
                }

                if (campo.Name.Equals("ClientAccounts"))
                {
                    poscompany = 0;
                    string where = "and ("; int p = 0;
                    XmlNodeList clientacc = campo.ChildNodes;
                    foreach (XmlElement acc in clientacc)
                    {

                        switch (acc.Name)
                        {
                            case "account":
                                {
                                    matcompany[poscompany] = int.Parse(acc.InnerText);
                                    poscompany++;
                                    if (p == 1) { where += " or "; } else { p = 1; }
                                    where += " account = " + acc.InnerText;
                                    break;
                                }
                        }
                    }
                    DB2.delete("tbl_usuario_account", "uacc_usu_id", usuarioid, 0, 1);
                    DB2.setAccountporUsuario(usuarioid, matcompany, poscompany);
                    DB2.updatedata("tbl_usuario", "usu_where3", "usu_id", usuarioid, 0, where, 1);
                }



                if (campo.Name.Equals("VendorStates"))
                {
                    posstate = 0;
                    XmlNodeList states = campo.ChildNodes;
                    foreach (XmlElement state in states)
                    {

                        switch (state.Name)
                        {
                            case "state":
                                {
                                    
                                    matstate[posstate]= int.Parse(state.InnerText);
                                    posstate++;
                                    break;
                                }
                        }
                    }
                    DB2.delete("tbl_usuario_state", "ust_usu_id", usuarioid, 0, 1);
                    DB2.setStateporUsuario(usuarioid, matstate, posstate);
                }




                if (campo.Name.Equals("VendorCounties"))
                {
                    poscounty = 0;
                    XmlNodeList counties = campo.ChildNodes;
                    foreach (XmlElement county in counties)
                    {

                        switch (county.Name)
                        {
                            case "county":
                                {
                                   
                                    matcounty[poscounty] = int.Parse(county.InnerText);
                                    poscounty++;
                                    break;
                                }
                        }
                    }

                    DB2.delete("tbl_usuario_county", "ucou_usu_id", usuarioid, 0, 1);
                    DB2.setCountyporUsuario(usuarioid, matcounty, poscounty);
                }

                if (campo.Name.Equals("ClientPrices"))
                {
                    posvprices = 0; matvprices[posvprices] = new string[2];
                    XmlNodeList clientprices = campo.ChildNodes;
                    foreach (XmlElement price in clientprices)
                    {
                        
                        switch (price.Name)
                        {
                            case "product":
                                {
                                    matvprices[posvprices][0] = price.InnerText;
                                    break;
                                }
                            case "price":
                                {
                                    matvprices[posvprices][1] = price.InnerText;
                                    posvprices++;
                                    matvprices[posvprices] = new string[2];
                                    break;
                                }
                                
                        }
                    }
                    DB2.delete("tbl_usuario_precio", "upr_usu_id", usuarioid, 0, 1);
                    DB2.setPrecioporUsuario(usuarioid, matvprices, posvprices);
                }

            
                    if (campo.Name.Equals("VendorPrices"))
                {
                    posvprices=0;
                    matvprices[posvprices] = new string[2];
                    XmlNodeList vendorprices = campo.ChildNodes;
                    foreach (XmlElement price in vendorprices)
                    {
                       
                        switch (price.Name)
                        {
                            case "product":
                                {
                                    
                                    matvprices[posvprices][0] = price.InnerText;
                                    break;
                                }
                            case "price":
                                {
                                    matvprices[posvprices][1] = price.InnerText;
                                    posvprices++;
                                    matvprices[posvprices] = new string[2];
                                    break;
                                }
                        }
                    }
                    DB2.delete("tbl_usuario_precio", "upr_usu_id", usuarioid, 0, 1);
                    DB2.setPrecioporUsuario(usuarioid, matvprices, posvprices);
                   }
            }

            resp = 1;
        }
            

        
        catch (Exception e)
        {
            string ruta = "c:\\TEMP\\archivo.txt";
            StreamWriter escritor = new StreamWriter(ruta, true);
            escritor.WriteLine(e.Message.ToString() + "  " + e.StackTrace.ToString());
            escritor.Close();
            resp = 0;
      
        }
        return resp;
    }
}
